package com.ysj.demo.mvpdemo.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ysj.demo.mvpdemo.R;
import com.ysj.demo.mvpdemo.bean.User;
import com.ysj.demo.mvpdemo.mvp.presenter.LoginPresenter;
import com.ysj.demo.mvpdemo.mvp.view.LoginView;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class MainActivity extends AppCompatActivity implements LoginView, View.OnClickListener {

    private EditText et_name, et_password;
    private Button btn_login;
    private TextView tv_message;
    private Button bt_message;

    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast.makeText(this, Thread.currentThread().getId() + " 主线程弹出框", Toast.LENGTH_SHORT).show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(MainActivity.this, Thread.currentThread().getId() + " 子线程弹出框", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }).start();

        initView();
        tv_message = (TextView) this.findViewById(R.id.tv_message);
        tv_message.setText("MainActivity");
        bt_message = (Button) this.findViewById(R.id.bt_message);
        bt_message.setText("跳转到SecondActivity");
        bt_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ThreeActivity.class));
            }
        });
        //注册事件
        EventBus.getDefault().register(this);
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_name);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
        loginPresenter = new LoginPresenter();

        loginPresenter.bind(this);//绑定View和Presente
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMoonEvent(Object object) {
        if (object instanceof String) {
            tv_message.setText((String) object);
        }
    }

    /**
     * 登录按钮的监听方法
     * 这里要做后台数据的处理，需要用到Presenter
     */
    @Override
    public void onClick(View v) {
        loginPresenter.login();
    }

    /**
     * 下面五个方法都是实现LoginView后要是实现的方法
     */

    @Override
    public String getAccount() {
        return et_name.getText().toString();
    }

    @Override
    public String getPassword() {
        return et_password.getText().toString();
    }

    @Override
    public void loginSuccess(User user) {
        //登录成功后，一般是实现页面的跳转
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void showNetworkError() {
        Toast.makeText(this, "当前网络不可用", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showVerifyFailed() {
        Toast.makeText(this, "输入的用户名或密码有误", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册事件
        EventBus.getDefault().unregister(this);
    }
}
